<?php
namespace app\wechat\home;
use app\common\controller\Common;
use app\wechat\model\Area as AreaModel;
use app\wechat\model\Seat as SeatModel;
use app\wechat\model\Open as OpenModel;
use app\wechat\model\Close as CloseModel;
use app\wechat\model\Reservation as ReservationModel;
use app\common\util\applet\Generate;
//座位控制器
class Seat extends Common
{
    /**
     * 区域座位查询
     * @param  string $stime 初始时间
     * @param  string $etime 结束时间
     */
    public function search(){
    	$data = input('post.');
        $result = $this->validate($data, 'Seat.areaSearch');
        if($result !== true) {
            return show(0,$result);
        }
        $stime = strtotime($data['stime']);
        $etime = strtotime($data['etime']);
        if ($stime <= time()){
            return show(0,'预约开始时间不能小于当前时间');
        }
        //判断是否在开馆时间内-start
        $num = $this->openWeek($stime,$etime);
        if ($num == 0) {
            return show(0,'不能预约闭馆时间');
        }
        //获取该时间段闭馆区域数组
        $closeAreaArr = $this->closeArea($stime,$etime);
        // 查询区域座位-start
    	$seat = new SeatModel();
    	$reservation = new ReservationModel();
    	$area = new AreaModel();
        $areaData = AreaModel::getArea();
        $searchAreaData = [];
        //查询区域
        if (isset($data['areaid']) && !empty($data['areaid'])){
            $childAreaArr = $area->getChildArea($data['areaid']);
            if (!empty($childAreaArr)) {
                $childAreaStr = self::getGroupStr($childAreaArr);
                $childAreaStr = $data['areaid'].$childAreaStr;
            }else{
                $childAreaStr = $data['areaid'];
            }
            $searchAreaArr = explode(',',$childAreaStr);
            foreach ($areaData as $va){
                if (in_array($va['id'],$searchAreaArr)){
                    $searchAreaData[] = $va;
                }
            }
        }else{
            $searchAreaData = $areaData;
        }
        //区域状态
    	foreach ($searchAreaData as $key => $value) {
    		$total = $seat->getTotal($value['id']);
            $reservationArr = $reservation->getUseCount($stime,$etime,$value['id']);
            $array = [];
            foreach ($reservationArr as $k => $v) {
                $array[]= $v['seatid'];
            }
            $existArr = array_unique($array);
            $existNum = count($existArr);
            $searchAreaData[$key]['total'] = $total;
            $searchAreaData[$key]['surplus'] = $total - $existNum;
            if (in_array($value['id'], $closeAreaArr)) {
                $searchAreaData[$key]['status'] = 0;
            }else{
                $searchAreaData[$key]['status'] = 1;
            }
    	}
        return show(1,'搜索成功',$searchAreaData);
    }

    /**
     *筛选两级区域列表
     */
    public function areaList(){
        $area = new AreaModel();
        $arealist = $area->getTwoArea();
        return show(1,'成功',$arealist);
    }
    /**
     * 座位布局查询
     * @param  string $stime 初始时间
     * @param  string $etime 结束时间
     * @param  int $areaid 区域id
     */
    public function seatLayout(){
        $data = input('post.');
        $result = $this->validate($data, 'Seat.seatLayout');
        if($result !== true) {
            return show(0,$result);
        }
        $stime = strtotime($data['stime']);
        $etime = strtotime($data['etime']);

        $seat = new SeatModel();
        $reservation = new ReservationModel();
        $seatArr = $seat->getSeatArr($data['areaid']);
        if (empty($seatArr)) {
            return show(0,'该区域暂未添加座位');
        }
        $seatTypeImg = [config('module_library.hard_seat_pic'),config('module_library.soft_seat_pic'),config('module_library.academic_pic'),config('module_library.exchange_pic'),config('module_library.selected_hard_seat_pic'),config('module_library.selected_soft_seat_pic'),config('module_library.selected_academic_pic'),config('module_library.selected_exchange_pic')];
        //获取该时间段已选位置id数组
        $reservationArr = $reservation->getUseCount($stime,$etime,$data['areaid']);
        $array = [];
        foreach ($reservationArr as $k => $v) {
            $array[]= $v['seatid'];
        }
        //获取背景图片
        $area = new AreaModel();
        $bgimg = $area->getAreaBgimg($data['areaid']);
        $existArr = array_unique($array);
        foreach ($seatArr as $key => $value) {
            if (in_array($value['id'], $existArr)) {
                $seatArr[$key]['status'] = 0;
            }else{
                $seatArr[$key]['status'] = 1;
            }
            switch ($value['seatType']) {
                case '1':$seatimg = $seatTypeImg[0];break;
                case '2':$seatimg = $seatTypeImg[1];break;
                case '3':$seatimg = $seatTypeImg[2];break;
                case '4':$seatimg = $seatTypeImg[3];break;
                default:$seatimg = '';break;
            }
            $seatArr[$key]['seatimg'] = $seatimg;
            switch ($value['seatType']) {
                case '1':$selectedSeatimg = $seatTypeImg[4];break;
                case '2':$selectedSeatimg = $seatTypeImg[5];break;
                case '3':$selectedSeatimg = $seatTypeImg[6];break;
                case '4':$selectedSeatimg = $seatTypeImg[7];break;
                default:$selectedSeatimg = '';break;
            }
            $seatArr[$key]['selectedSeatimg'] = $selectedSeatimg;
        }
        $layoutData['bgimg'] = $bgimg;
        $layoutData['seat'] = $seatArr;
        return show(1,'成功',$layoutData);
    }
    /**
     * 判断是否在开馆时间时间
     * @param  string $stime 初始时间
     * @param  string $etime 结束时间
     */
    public function openWeek($stime,$etime){
        $open = new OpenModel();
        $morrow = strtotime(date('Y-m-d',strtotime('+1 day')));
        $week = date('w');
        if ($stime < $morrow) {
            // 预约当天
            $week = $week;
            $weekArr = $open->getOpenTime($week);
            $opentime = getWeekTime($weekArr);
            $num = 0;
            foreach ($opentime as $ka => $va) {
                if ( strtotime($va['starttime']) <= $stime && $etime <= strtotime($va['endtime']) ) {
                    $num = 1;
                }
            }
        }else{
            // 预约明天
            $week = $week == 6 ? 0 : $week+1;
            $weekArr = $open->getOpenTime($week);
            $opentime = getWeekTime($weekArr);
            $num = 0;
            foreach ($opentime as $ka => $va) {
                if ( strtotime($va['starttime'])+86400 <= $stime && $etime <= strtotime($va['endtime'])+86400 ) {
                    $num = 1;
                }
            }
        }
        return $num;
    }
    /**
     * 获取该时间段闭馆区域数组
     * @param  string $stime 初始时间
     * @param  string $etime 结束时间
     * @param  int $areaid 区域id
     */
    public function closeArea($stime,$etime){
        $Close = new CloseModel();
        $closeArr = $Close->getCloseArea($stime,$etime);
        $areaArr = [];
        foreach ($closeArr as $key => $value) {
            $arr = explode(',', $value['group']);
            $areaArr = array_merge($areaArr,$arr);
        }
        $areaArr = array_unique($areaArr);
        return $areaArr;
    }
    /**
     * 把多维数组转为字符串id集
     * @return string
     */
    private function getGroupStr($group){
        $str = '';
        foreach ($group as $value) {
            $str .= ','.$value['id'];
            if (!empty($value['child'])) {
                $str .= self::getGroupStr($value['child']);
            }
        }
        return $str;
    }
    public function test(){

        $time = strtotime('2018-8-14 10:00');
        return 'ssssss';
    }
}